﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace E.Y.E.Jobs
{
    class CommandEditKeyword : ICommand
    {
        private const string MESSAGE_COMMAND_TITLE = "/=======EDIT KEYWORD COMMAND=======\\";
        private const string MESSAFE_UNDO_EDIT_KEYWORD = "Undo: Keyword \"{0}\" is restored.";
        private const string MESSAGE_SUCCESSFUL_EDIT_KEYWORD = "Keyword:\"{0}\" is changed to {1}";
        private const string ERROR_MESSAGE_FAIL_EDITTING_KEYWORD = "So embarrasing. Error occured while we are trying to restore your keyword.";

        ArrayList OriginalDatabase;
        String Message;
        bool ErrorFlag;
        CommandTypeManagerEnumClass.CommandType EditKeyType;
        string EditKeyword;

        public CommandEditKeyword(CommandTypeManagerEnumClass.CommandType MainType, string customizedKeyword)
        {
            OriginalDatabase = new ArrayList();
            EditKeyType = MainType;
            EditKeyword = customizedKeyword;
            Message = "";
            ErrorFlag = true;
        }

        public bool Execute(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            OriginalDatabase = CommandTypeDatabase.GetCurrList();
            bool isEmptyKeyword = EditKeyword == "";
            if (isEmptyKeyword)
            {
                Message = ERROR_MESSAGE_FAIL_EDITTING_KEYWORD;
                ErrorFlag = true;
                return false;
            }

            bool KeywordEditted = CommandTypeDatabase.EditCommandType(EditKeyType, EditKeyword);
            if (KeywordEditted)
            {
                actionsHistory.AddNewHistory(this);
                Message = String.Format(MESSAGE_SUCCESSFUL_EDIT_KEYWORD, EditKeyword, CommandTypeDatabase.TypeToString(EditKeyType));
                ErrorFlag = false;
                return false;
            }
            else
            {
                Message = ERROR_MESSAGE_FAIL_EDITTING_KEYWORD;
                ErrorFlag = true;
                return false;
            }
        }

        public bool Undo(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            try
            {
                bool UndoSuccess = CommandTypeDatabase.RestoreData(OriginalDatabase);
                Message = String.Format(MESSAFE_UNDO_EDIT_KEYWORD, EditKeyword);
                ErrorFlag = false;
                return UndoSuccess;
            }
            catch
            {
                return false;
            }
        }

        public bool setKeyword(string customizedKeyword)
        {
            EditKeyword = customizedKeyword;
            return true;
        }

        public string getCommandEventTitle()
        {
            return MESSAGE_COMMAND_TITLE;
        }

        public void setMessage(string desiredMessage)
        {
            Message = desiredMessage;
        }

        public string getMessage()
        {
            return Message;
        }

        public void SetError(bool desiredBool)
        {
            ErrorFlag = true;
        }

        public bool GetErrorState()
        {
            return ErrorFlag;
        }

        public InformationPackage ConvertToInfoPackage()
        {
            InformationPackage Result = new InformationPackage();

            if (ErrorFlag)
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.OperationError;
            }
            else
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.AddKeyword;
            }
            Result.Message = Message;

            return Result;
        }
    }
}
